package com.bogu.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bogu.adapter.UserListAdapter1;
import com.bogu.constant.ConstantS;
import com.bogu.dao.UserDao;
import com.example.boguplatform.R;
import com.xuan.tuya.BasicActivity;
import com.xuan.tuya.InjectView;
import com.bogu.model.User;
public class SearchUserActivity extends BasicActivity {
	@InjectView(R.id.userlist)
	private ListView list;
	@InjectView(R.id.hint)
	private TextView hint;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private UserDao userDao;
	private SharedPreferences preferences;
	private ArrayList<User> users;
	private UserListAdapter1 userAdapter;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO 自动生成的方法存根
			switch (msg.what) {
			case 0:
				progressBar.setVisibility(View.VISIBLE);
				break;
			case 1:
				progressBar.setVisibility(View.GONE);
                 users=(ArrayList<User>) msg.obj;
                 if(users.size()>0)
                 {hint.setVisibility(View.GONE);
                	 userAdapter=new UserListAdapter1(SearchUserActivity.this,users);
                	 list.setAdapter(userAdapter);
                 }else{hint.setVisibility(View.VISIBLE);}
				break;
			case 2:
				progressBar.setVisibility(View.GONE);
				break;
			
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_searchuser);
		preferences = getSharedPreferences(ConstantS.KEY_SETTING,
				Context.MODE_PRIVATE);
		userDao = new UserDao(this, handler);
		userDao.searchUser(preferences.getInt(ConstantS.KEY_UID, 0),
				getIntent().getStringExtra("keyword"));
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO 自动生成的方法存根
				Intent intent=new Intent(SearchUserActivity.this,UserActivity.class);
				intent.putExtra("type", "");
				intent.putExtra("user", users.get(position));
				startActivityForResult(intent, 0);
			}
		
		});
		initActionBar();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO 自动生成的方法存�?
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		}
		return super.onOptionsItemSelected(item);
	}

	private void initActionBar() {

		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);
		getSupportActionBar().setDisplayUseLogoEnabled(false);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle("搜索用户");
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO 自动生成的方法存根
		switch(arg1){
		case RESULT_OK:
			userDao.searchUser(preferences.getInt(ConstantS.KEY_UID, 0),
					getIntent().getStringExtra("keyword"));
			break;
		}
	}
	
}
